<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="renderer" content="webkit">
<title>浏览器-访客分析</title>

<script type="text/javascript" src="../resources/js/contants.js"></script>
<link href="../resources/css/bootstrap.min.css" rel="stylesheet">
<link href="../resources/css/font-awesome.min.css" rel="stylesheet">
<link href="../resources/css/style.min.css" rel="stylesheet">
<link href="../resources/css/dateRange.css" rel="stylesheet">
<link href="../resources/css/bootstrap-select.min.css" rel="stylesheet">
<link rel="stylesheet" href="../resources/css/layer.css" id="layui_layer_skinlayercss" style="">
<link rel="stylesheet" href="../resources/css/layer.ext.css" id="layui_layer_skinlayercss" style="">
<link rel="stylesheet" href="../resources/css/style.css" id="layui_layer_skinlayercss" style="">
</head>

<body class="gray-bg">
    <div class="wrapper wrapper-content">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <span class="label label-info">公用过滤条件</span>
                    </div>
                    <div class="ibox-content">
                        <span class="font-size-h1">国家:
                            <select id="location_country_select">
                            </select>
                        </span>
                        <span class="font-size-h1">省份:
                            <select id="location_province_select">
                            </select>
                        </span>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-sm-4">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <span class="label label-success pull-right">昨日</span>
                        <h5>总会话个数</h5>
                    </div>
                    <div class="ibox-content">
                        <h1 class="no-margins"><span id="location_sessions">-</span></h1>
                    </div>
                </div>
            </div>
            <div class="col-sm-4">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <span class="label label-success pull-right">昨日</span>
                        <h5>只访问一次的会话个数</h5>
                    </div>
                    <div class="ibox-content">
                        <h1 class="no-margins"><span id="location_bounce_number">-</span></h1>
                    </div>
                </div>
            </div>
            <div class="col-sm-4">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <span class="label label-success pull-right">昨日</span>
                        <h5>跳出率</h5>
                    </div>
                    <div class="ibox-content">
                        <h1 class="no-margins"><span id="location_bounce_rate">-</span></h1>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div id="demo_title" class="col-sm-12 no-margins">
              <div class="row no-paddings no-margins no-border">
                <div class="col-sm-12 no-margins" style="padding:0px; border:0px;">
                  <div class="ibox no-margins">
                    <div class="ibox-content">
                        <span class="no-margins">选择时间：</span>
                        <button id="time_show_bt" type="button" class="btn btn-default no-margins no-borders no-paddings" style="padding-bottom: 0px">
                            <span id="date1">默认</span>
                            <span class="caret"></span>
                        </button>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <div id="demo" class="col-sm-12"></div>
            <div id="demo1" class="col-sm-12"></div>
        </div>
    </div>

    <script type="text/javascript" src="../resources/js/jquery-2.1.1.min.js"></script>
    <script type="text/javascript" src="../resources/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="../resources/js/dateRange.js"></script>
    <script type="text/javascript" src="../resources/js/bootstrap-select.min.js"></script>
    <!-- highcharts js -->
    <script type="text/javascript" src="../resources/js/highcharts.js"></script>
    <script type="text/javascript" src="../resources/js/exporting.js"></script>
    <script type="text/javascript" src="../resources/js/json2.js"></script>
    <script type="text/javascript" src="../resources/js/common.js"></script>

    <script type="text/javascript">
        var startDate = getStartDate();
        var endDate = getEndDate();

        $("#location_country_select").change(function(){
            var country = $(this).val();
            var province = $("#location_province_select").val();
            osChangeEvent(country, province);
        });
        $("#location_province_select").change(function(){
            var country = $("#location_country_select").val();
            var province = $(this).val();
            osChangeEvent(country, province);
        });

        function osChangeEvent(country, province) {
            var sd = date2YmdStr(getYesterDay());
            var ed = sd;
            loadHeadData(sd, ed, country, province)
            loadContent(getStartDate(), getEndDate(), country, province);
        };

        function loadHeadData(sd, ed, country, province) {
            var url = COMMON_PREFIX_URL + "/stats/summary/location?metric=visit,bounce_number&platform=website";
            url += "&start_date=" + sd + "&end_date=" + ed;
            if (country) {
                url += "&country=" + encodeURIComponent(country); 
            }
            if (province) {
                url += "&province=" + encodeURIComponent(province);
            }

            $.get(url, {}, function(data){
                if (data.code == '200' && data.data.length > 0) {
                    var usersData = data.data[0];
                    var visit = usersData["$visit"];
                    if (typeof(visit) != "undefined") {
                        $("#location_sessions").html(visit.formatThousands());
                    }
                    var bounceNumber = usersData["$bounce_number"];
                    if (typeof(bounceNumber) != "undefined") {
                        $("#location_bounce_number").html(bounceNumber.formatThousands());
                    }
                    $("#location_bounce_rate").html(calculateBounceRate(bounceNumber, visit).toPercent() + "%");
                } else {
                    $("#location_sessions").html("-");
                    $("#location_bounce_number").html("-");
                    $("#location_bounce_rate").html("-");
                }
            });
        };

        function loadContent(sd, ed, country, province) {
            var url = COMMON_PREFIX_URL + "/stats/groupBy/location?metric=visit,bounce_number&platform=website";
            url += "&start_date=" + sd + "&end_date=" + ed + "&group_by=day";
            if (country) {
                url += "&country=" + encodeURIComponent(country);
            }
            if (province) {
                url += "&province=" + encodeURIComponent(province);
            }

            $.get(url, {}, function(data) {
                cleanContent("demo");
                cleanContent("demo1");
                cleanContent("demo2");

                if (data.code == '200') {
                    if (data.data.length == 0) {
                       showNoConnent("demo");
                       return;
                    }

                    var categories = [];
                    var totalVisit = {
                        name: "总会话",
                        data: []
                    };
                    var bounceVisit = {
                        name: "跳出会话",
                        data: []
                    };
                    var bounceRate = {
                        name: "跳出率",
                        data: []
                    };

                    $.each(data.data, function(i, item) {
                        var day = item["day"];
                        categories.push(day);
                        var tv = item["$visit"];
                        var bv = item["$bounce_number"];
                        tv = tv ? tv : 0;
                        bv = bv ? bv : 0;

                        totalVisit["data"].push(tv);
                        bounceVisit["data"].push(bv);
                        bounceRate["data"].push(calculateBounceRate(bv, tv).toPercent());
                    });

                    var chart = new Highcharts.Chart({
                        chart : {
                            renderTo : 'demo', // 在那个标签上显示，指定id即可
                            type : "column" // 类型
                        },
                        xAxis : {
                            title : {
                                text : "日期"
                            }
                        },
                        title: {
                            text: "总会话和只访问一次的会话比较"
                        },
                        yAxis : [{
                            min: 0,
                            title : {
                                text : "会话个数"
                            }
                        }],
                        series : []
                    });
                    var chart2 = new Highcharts.Chart({
                        chart : {
                            renderTo : 'demo1', // 在那个标签上显示，指定id即可
                        },
                        xAxis : {
                            title : {
                                text : "日期"
                            }
                        },
                        yAxis : [{
                            title : {
                                text : "百分比(%)"
                            }
                        }],
                        title: {
                            text: "跳出率变化趋势"
                        },
                        tooltip: {
                            valueSuffix: ' %'
                        },
                        series : []
                    });
                    chart.addSeries(totalVisit);
                    chart.addSeries(bounceVisit);
                    chart.xAxis[0].setCategories(categories);
                    chart2.addSeries(bounceRate);
                    chart2.xAxis[0].setCategories(categories);
                    return true;
                } else {
                    showNoConnent("demo", "获取数据失败，失败code为:" + data.code + ", 失败信息描述为:" + data.msg);
                }
                return false;
            }, "json");
        }

        $(document).ready(function(){
            // 加载省份基本信息
            var url = COMMON_PREFIX_URL + "/stats/getDimensions?dimensions=location_province";
            $.get(url, {}, function(data){
                if (data.code == '200' && data.data != null && data.data["location_province"].length > 0) {
                    var countrys = [];
                    var str = '<option value="all">all</option>';
                    $.each(data.data["location_province"], function(i, item) {
                        if (item != 'all') {
                            str += '<option value="' + item["province"] + '">' + item["province"] + '</option>'
                            countrys.push(item["country"]);
                        }
                    });
                    $("#location_province_select").html(str);
                    $("#location_province_select").addClass("selectpicker");
                    countrys = uniqueArray(countrys);
                    var cstr = "";
                    $.each(countrys, function(i, item){
                        cstr += '<option value="' + item + '">' + item + '</option>';
                    });
                    $("#location_country_select").html(cstr);
                    $("#location_country_select").addClass("selectpicker");
                    $('.selectpicker').selectpicker();
                } else {
                }
            });

            var sd = date2YmdStr(getYesterDay());
            var ed = sd;
            loadHeadData(sd, ed, "中国");
            loadContent(startDate, endDate, "中国");

            // 显示时间控件
            new AE_Date('date1','time_show_bt',function(startDate, endDate){
                loadContent(startDate, endDate, $("#location_country_select").val(), $("#location_province_select").val());
            }, startDate, endDate);
        });

        function calculateBounceRate(bounceNumber, visit) {
            if (visit) {
                return bounceNumber / visit;
            } else {
                return Number(0);
            }
        };
        
    </script>
</body>
</html>